/*
 * ResourceManager.cpp
 *
 *  Created on: May 11, 2012
 *      Author: mklingen
 */

#include "ResourceManager.h"

namespace kling
{

    ResourceManager::ResourceManager()
    {

    }

    ResourceManager::~ResourceManager()
    {
        ClearResources();
    }

    void ResourceManager::RegisterResource(std::string name, kling::Resource* resource)
    {
        if(HasResource(name))
        {
            UnregisterResource(name);
        }

        m_resourceMap[name] = resource;
    }

    void ResourceManager::UnregisterResource(std::string name)
    {
        if(!HasResource(name))
        {
            return;
        }

        delete m_resourceMap[name];
        m_resourceMap.erase(m_resourceMap.find(name));
    }

    void ResourceManager::ClearResources()
    {
        std::vector<std::string> keys;
        for(std::map<std::string, kling::Resource*>::iterator it = m_resourceMap.begin(); it != m_resourceMap.end(); it++)
        {
            keys.push_back(it->first);
        }

        for(std::vector<std::string>::iterator it = keys.begin(); it != keys.end(); it++)
        {
            UnregisterResource(*it);
        }
    }

    bool ResourceManager::IsResourceInitialized(std::string name)
    {
        if(!HasResource(name))
        {
            return false;
        }

        Resource* resource = m_resourceMap[name];
        return resource->IsInitialized();
    }

    bool ResourceManager::InitializeResource(std::string name)
    {
        if(IsResourceInitialized(name))
        {
            return true;
        }
        else if(HasResource(name))
        {
            return m_resourceMap[name]->Initialize();
        }
        else
        {
            return false;
        }
    }

} /* namespace arms */
